#include <cstring>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "command.hpp"

int main()
{
  //usmask(0) //可以更改默认权限掩码
  int n = mkfifo(fifoName.c_str(), mode);
  if(n == -1)
  {
    cout << "error: mkfifo fail! errno:" << errno << " : " << strerror(errno) << endl; 
    return 1;
  }
  cout << "mkfifo sucess" << endl;

  int rfd = open(fifoName.c_str(), O_RDONLY); //以只读的方式打开命名管道文件
  if(rfd == -1)
  {
    cout << "error: open fail! errno:" << errno << " : " << strerror(errno) << endl;
    return 2;
  }
  cout << "open success, begin IPC" << endl;

  char buff[NUM];
  while(true)
  {
    buff[0] = 0;  //缓冲区清空
    ssize_t n = read(rfd, buff, sizeof(buff) - 1);

    if(n > 0)
    {
      buff[n] = 0;
      printf("%s", buff);
      fflush(stdout);
    }
  }

  return 0;
}
